package com.amumu.drama.admin.modules.member.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.amumu.drama.admin.modules.member.dto.req.UmsMemberListQuery;
import com.amumu.drama.admin.modules.member.dto.req.UmsMemberStatusReq;
import com.amumu.drama.admin.modules.member.dto.resp.UmsMemberDetail;
import com.amumu.drama.admin.modules.member.dto.resp.UmsMemberListResp;
import com.amumu.drama.admin.modules.member.mapping.UmsMemberDetailMapping;
import com.amumu.drama.admin.modules.member.mapping.UmsMemberListRespMapping;
import com.amumu.drama.admin.modules.member.service.UmsAdminMemberService;
import com.amumu.drama.common.base.api.CommonPage;
import com.amumu.drama.common.constant.InvitationTypeEnum;
import com.amumu.drama.common.exception.ApiException;
import com.amumu.drama.mapper.InvitationMapper;
import com.amumu.drama.mapper.OmsOrderMapper;
import com.amumu.drama.mapper.UmsMemberMapper;
import com.amumu.drama.model.UmsMember;
import com.amumu.drama.model.UmsMemberExample;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
public class UmsAdminAdminMemberServiceImpl implements UmsAdminMemberService {

    @Autowired
    private UmsMemberMapper umsMemberMapper;
    @Autowired
    private InvitationMapper invitationMapper;
    @Autowired
    private OmsOrderMapper omsOrderMapper;

    @Override
    public List<UmsMember> search(String phoneNum, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        UmsMemberExample example = new UmsMemberExample();
        UmsMemberExample.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(phoneNum)) {
            criteria.andPhoneEqualTo(phoneNum);
        }
        example.setOrderByClause("id desc");
        return umsMemberMapper.selectByExample(example);
    }

    @Override
    public CommonPage<UmsMemberListResp> queryListUmsMemberByPage(UmsMemberListQuery request) {
        PageHelper.startPage(request.getPageNum(), request.getPageSize());
        QueryWrapper<UmsMember> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StrUtil.isNotBlank(request.getPhone()), "phone", request.getPhone())
                .eq(request.getStatus() != null, "status", request.getStatus())
                .eq(ObjectUtil.isNotNull(request.getMemberId()), "id", request.getMemberId())
                .eq(StrUtil.isNotBlank(request.getInvitationCode()), "invitation_code", request.getInvitationCode())
                .eq(ObjectUtil.isNotNull(request.getInvitationStatus()), "invitation_status", request.getInvitationStatus())
                .orderByDesc("id");
        List<UmsMember> umsMembers = umsMemberMapper.selectList(queryWrapper);
        return CommonPage.restPageAndConvert(umsMembers, UmsMemberListRespMapping.INSTANCE::convertTo);
    }

    @Override
    public UmsMemberDetail getMemberDetail(Long memberId) {
        UmsMember member = umsMemberMapper.selectById(memberId);
        UmsMemberDetail umsMemberDetail = UmsMemberDetailMapping.INSTANCE.convertTo(member);
        //查询用户游戏下单统计
        umsMemberDetail.setGameOrderCountList(omsOrderMapper.selectGameOrderStatistics(InvitationTypeEnum.MEMBER.getCode(), memberId + ""));
        return umsMemberDetail;
    }

    @Override
    public void enableStatus(UmsMemberStatusReq request) {
        UmsMember member = umsMemberMapper.selectById(request.getMemberId());
        if (member == null) {
            throw new ApiException("用户不存在");
        }
        if (request.getStatus() != null) {
            //更新用户状态
            if (!Objects.equals(request.getStatus(), 1) && !Objects.equals(request.getStatus(), 0)) {
                throw new ApiException("封禁/解封用户状态不合法");
            }
            UmsMember updateMember = new UmsMember();
            updateMember.setId(request.getMemberId());
            updateMember.setStatus(request.getStatus());
            umsMemberMapper.updateById(updateMember);
        }

        if (request.getInvitationStatus() != null) {
            //更新邀请状态
            if (!Objects.equals(request.getInvitationStatus(), 1) && !Objects.equals(request.getInvitationStatus(), 0)) {
                throw new ApiException("邀请奖励状态不合法");
            }
            UmsMember updateMember = new UmsMember();
            updateMember.setId(request.getMemberId());
            updateMember.setInvitationStatus(request.getInvitationStatus());
            umsMemberMapper.updateById(updateMember);
        }
    }
}
